package com.hnau.chpater9;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class ActivityThreadPicture extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_thread_media);
		final ImageView iv = (ImageView)super.findViewById(R.id.imageView1);
		new Thread(
				new Runnable(){
					public void run() {
						final Bitmap bitmap = getPicture("http://10.0.0.2:8080/quartz/images/kong.jpg");
						try{
							Thread.sleep(2000);
						}catch(Exception e){
							e.printStackTrace();
						}
						iv.post(new Runnable(){
							@Override
							public void run() {
								iv.setImageBitmap(bitmap);
							}
							
						});
					}
				}
		).start();
	}

	public Bitmap getPicture(String path){
		Bitmap bm = null;
		try {
			URL url = new URL(path);
			URLConnection conn = url.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			bm = BitmapFactory.decodeStream(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bm;
	}
	
}
